package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.AfterSaleService;
import com.lanchetech.bean.request.AfterSalePageReq;
import com.lanchetech.bean.response.*;
import com.lanchetech.entity.AfterSale;
import com.lanchetech.service.CommonAfterSaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(value = "售后管理接口", tags = "售后管理接口")
@RequestMapping("/api/after-sale")
@RestController
public class AfterSaleController {

    @Autowired
    AfterSaleService afterSaleService;

    @Autowired
    CommonAfterSaleService commonAfterSaleService;

    @ApiOperation("获取售后分页")
    @PreAuthorize("hasAuthority('order.after-sale-list')")
    @PostMapping("/getAfterSalePage")
    public ResultData<BasePageResp<AfterSaleResp>> getAfterSalePage(@RequestBody AfterSalePageReq req) {
        return afterSaleService.getAfterSalePage(req);
    }

    @OperatorLog("导出所有售后Excel")
    @ApiOperation(value = "导出所有售后Excel")
    @PreAuthorize("hasAuthority('order.after-sale-list.export.auth')")
    @PostMapping("/exportAfterSaleExcel")
    public void exportAfterSaleExcel(@RequestBody AfterSalePageReq req, HttpServletResponse response) throws IOException {
        afterSaleService.exportAfterSaleExcel(req, response);
    }

    @ApiOperation("获取售后详情")
    @PreAuthorize("hasAuthority('order.after-sale-detail')")
    @GetMapping("/getAfterSaleDetail")
    public ResultData<AfterSaleDetailResp> getAfterSaleDetail(@RequestParam("id") Long id) {
        return afterSaleService.getAfterSaleDetail(id);
    }

    @OperatorLog("同意换货并编辑换货快递物流信息")
    @ApiOperation("同意换货并编辑换货快递物流信息")
    @PreAuthorize("hasAuthority('order.after-sale-list.edit.auth')")
    @PostMapping("/editAfterSaleLogistics")
    public BaseResp editAfterSaleLogistics(@RequestBody AfterSale req) {
        return afterSaleService.editAfterSaleLogistics(req);
    }

    @OperatorLog("拒绝售后申请")
    @ApiOperation("拒绝售后申请")
    @PreAuthorize("hasAuthority('order.after-sale-list.edit.auth')")
    @PostMapping("/rejectAfterSale")
    public BaseResp rejectAfterSale(@RequestBody AfterSale req) {
        return afterSaleService.rejectAfterSale(req);
    }

    @OperatorLog("同意售后，等待退货")
    @ApiOperation("同意售后，等待退货")
    @PreAuthorize("hasAuthority('order.after-sale-list.edit.auth')")
    @PostMapping("/confirmAfterSale")
    public BaseResp confirmAfterSale(@RequestBody AfterSale req) {
        return afterSaleService.confirmAfterSale(req);
    }

    @OperatorLog("同意退款")
    @ApiOperation("同意退款")
    @PreAuthorize("hasAuthority('order.after-sale-list.edit.auth')")
    @PostMapping("/agreeRefund")
    public BaseResp agreeRefund(@RequestBody AfterSale req) {
        return commonAfterSaleService.commonAgreeRefund(req);
    }

    @ApiOperation(value = "退款处理回调【微信支付退款回调】")
    @RequestMapping(value = "/refundNotify", method = {RequestMethod.GET, RequestMethod.POST})
    public String refundNotify(@RequestBody String xmlData) {
        return afterSaleService.refundNotify(xmlData);
    }

}
